Adding file descriptor in template.
[and.git] / 820 - Internet bandwidth / 820.cpp
blobcda25f0409a85a99a6211270c3e30ea830e8bd11
1 /*
2 Accepted
3 */
4 #include <iostream>
5 #include <iterator>
6 #include <queue>
7 #include <stdio.h>
9 using namespace std;
11 const int MAXN = 101;
13 int cap[MAXN+1][MAXN+1], flow[MAXN+1][MAXN+1], prev[MAXN+1];
15 int fordFulkerson(int n, int s, int t){
16 int ans = 0;
17 for (int i=0; i<n; ++i) fill(flow[i], flow[i]+n, 0);
18 while (true){
19 fill(prev, prev+n, -1);
20 queue<int> q;
21 q.push(s);
22 while (q.size() && prev[t] == -1){
23 int u = q.front();
24 q.pop();
25 for (int v = 0; v<n; ++v)
26 if (v != s && prev[v] == -1 && cap[u][v] > 0 && cap[u][v] - flow[u][v] > 0)
27 prev[v] = u, q.push(v);
30 if (prev[t] == -1) break;
32 int bottleneck = INT_MAX;
33 for (int v = t, u = prev[v]; u != -1; v = u, u = prev[v]){
34 bottleneck = min(bottleneck, cap[u][v] - flow[u][v]);
36 for (int v = t, u = prev[v]; u != -1; v = u, u = prev[v]){
37 flow[u][v] += bottleneck;
38 flow[v][u] = -flow[u][v];
40 ans += bottleneck;
42 return ans;
45 int main(){
46 int n, runs = 1;
47 while (scanf("%d", &n) == 1 && n){
48 for (int i=0; i<n; ++i){
49 fill(cap[i], cap[i]+n, 0);
51 int s, t, C;
52 scanf("%d %d %d", &s, &t, &C);
53 --s, --t;
54 while (C--){
55 int i, j, k;
56 scanf("%d %d %d", &i, &j, &k);
57 cap[--i][--j] += k, cap[j][i] += k;
60 printf("Network %d\nThe bandwidth is %d.\n\n", runs++, fordFulkerson(n, s, t));
62 return 0;